package com.gitee.wsl.time.chinese

import com.gitee.wsl.time.minusDays

import com.gitee.wsl.time.plusYears
import com.gitee.wsl.time.withDayOfYear
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.LocalTime


class ChineseHoliday(
    val year: Int,
    val specificHolidays: String,
    val specificWorkday: String
) {

    var specificHolidayUrl: String = "https://gameboylv.github.io/ChineseHoliday/data/%s.txt"
    var specificWorkdayUrl: String = "https://gameboylv.github.io/ChineseHoliday/data/%s_w.txt"

    var workStartTime: LocalTime = LocalTime(9, 0)
    var workEndTime: LocalTime = LocalTime(17, 30)

    val holiday  = ArrayList<LocalDate>()

    init {
        holidayOfYear()
    }

    private fun holidayOfYear() {

        val firstDayOfYear = LocalDate(year, 1, 1)
        val daysOfYear: Int = firstDayOfYear.plusYears(1).minusDays(1).dayOfYear

        for (i in 1..daysOfYear) {
            val day: LocalDate = firstDayOfYear.withDayOfYear(i)
            if (day.dayOfWeek == DayOfWeek.SATURDAY || day.dayOfWeek == DayOfWeek.SUNDAY)
                holiday.add(day)
        }

        try {
            /*val client: HttpClient = HttpClient.newHttpClient()

            val requestHolidays: java.net.http.HttpRequest = java.net.http.HttpRequest.newBuilder()
                .uri(URI.create(String.format(specificHolidayUrl, year)))
                .build()
            val specificHolidays: Unit =
                client.send(requestHolidays, java.net.http.HttpResponse.BodyHandlers.ofString())
                    .body()*/
            for (i in specificHolidays.split("\\n")) {
                if (i.length != 4) continue
                val day: LocalDate = LocalDate(
                    year,
                    i.substring(0, 2).toInt(),
                    i.substring(2, 4).toInt()
                )
                if (!holiday.contains(day)) holiday.add(day)
            }

            /*val requestWorkdays: java.net.http.HttpRequest = java.net.http.HttpRequest.newBuilder()
                .uri(URI.create(String.format(specificWorkdayUrl, year)))
                .build()
            val specificWorkday: Unit =
                client.send(requestWorkdays, java.net.http.HttpResponse.BodyHandlers.ofString())
                    .body()*/
            for (i in specificWorkday.split("\\n")) {
                if (i.length != 4) continue
                val day: LocalDate = LocalDate(
                    year,
                    i.substring(0, 2).toInt(),
                    i.substring(2, 4).toInt()
                )
                if (holiday.contains(day)) holiday.remove(day)
            }
        } catch (_: Exception) {
        }
        holiday.sort()
        //return holiday.toArray<LocalDate>(java.util.function.IntFunction<Array<LocalDate>> { _Dummy_.__Array__() })
    }

    fun getHoliday(year: Int, month: Int): List<LocalDate> {
        return holiday.filter { item: LocalDate ->
            item.monthNumber == month
        }
        /*return java.util.Arrays.stream<LocalDate>(getHoliday(year))
            .filter(java.util.function.Predicate<LocalDate> { item: LocalDate ->
                item.getMonth().getValue() == month
            })
            .toArray<LocalDate>(java.util.function.IntFunction<Array<LocalDate>> { _Dummy_.__Array__() })*/
    }

    fun isWorkingDay(date: LocalDate): Boolean {
        return !holiday.contains(date)
    }

    fun isWorkingTime(dateTime: LocalDateTime): Boolean {
        val date: LocalDate = dateTime.date
        val time: LocalTime = dateTime.time
        return isWorkingDay(date) && time > workStartTime && time < workEndTime
    }

    companion object{

        var holidayMap  = HashMap<Int, List<LocalDate>>()

        val Sample_2024 = """
            0101

            0212
            0213
            0214
            0215
            0216

            0404
            0405

            0501
            0502
            0503

            0610

            0916
            0917

            1001
            1002
            1003
            1004
            1007
        """.trimIndent()

        /*fun getHoliday(year: Int): List<LocalDate> {
            if (!holidayMap.containsKey(year)) holidayMap[year] = holidayOfYear(year)
            return holidayMap[year] ?: emptyList()
        }*/
    }
}